<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Jobs\QiniuUploadFile;
use App\Models\Pageadver;
use App\Models\Qiniutask;
use Illuminate\Http\Request;

class PageadverController extends Controller {

	public function __construct() {
		$this->middleware('role:pageadver');
	}

	/**
	 * 进入页面广告图片列表页面
	 */
	public function getIndex() {
		return view('admin/pageadver/index');
	}

	/**
	 * 获取分页数据
	 */
	public function getPageRows(Request $request) {
		$kw = strval($request->input('kw'));
		$page = to_limit_lng($request->input('page'), 1);
		$limit = to_limit_lng($request->input('limit'), 1);
		$offset = ($page - 1) * $limit;

		$query = Pageadver::where('title', 'LIKE', '%' . $kw . '%');
		$total_rows = $query->count();
		$rows = $query->orderBy('id', 'ASC')->skip($offset)->take($limit)->get();
		foreach ($rows as $row) {
			$row->pic = empty($row->pic) ? '' : asset($row->pic);
		}
		$data['rows'] = $rows;

		$data['total_rows'] = $total_rows;
		$data['page_count'] = ceil($total_rows / $limit);
		$data['page'] = $page;

		return $this->response($data);
	}

	/**
	 * 进入添加页面
	 */
	public function getAdd() {
		$token = session('token');
		if ($token['isHidden'] == false) {
			return $this->warning('无此操作权限！');
		}
		return view('admin/pageadver/add');
	}

	/**
	 * 新增记录
	 */
	public function postInsert(Request $request) {
		$token = session('token');
		if ($token['isHidden'] == false) {
			return $this->error('Access denied.');
		}
		$row = new Pageadver;
		$row->title = safe($request->input('title'));
		$row->width = intval($request->input('width'));
		$row->height = intval($request->input('height'));
		$row->status = to_limit_lng($request->input('status'), 0, 1);
		$row->save();
		return $this->response($row);
	}

	/**
	 * 进入编辑页面
	 */
	public function getEdit(Request $request) {
		$id = intval($request->input('id'));
		$row = Pageadver::find($id);
		if (empty($row)) {
			return $this->warning('无效的记录');
		}
		return view('admin/pageadver/edit', $row);
	}

	/**
	 * 修改记录
	 */
	public function postUpdate(Request $request) {
		$id = intval($request->input('id'));
		$row = Pageadver::find($id);
		if (empty($row)) {
			return $this->error('无效的记录');
		}

		if ($request->exists('title')) {
			$row->title = safe($request->input('title'));
		}
		if ($request->exists('width')) {
			$row->width = intval($request->input('width'));
		}
		if ($request->exists('height')) {
			$row->height = intval($request->input('height'));
		}
		if ($request->exists('status')) {
			$row->status = to_limit_lng($request->input('status'), 0, 1);
		}
		if ($row->save()) {
			return $this->response($row);
		}
		return $this->error('操作失败');
	}

	/**
	 * 上传图片
	 */
	public function postUploadPic(Request $request) {
		$id = intval($request->input('id'));
		$row = Pageadver::find($id);
		if (empty($row)) {
			return $this->response('无效的记录');
		}

		$imagedata = file_get_contents('php://input');
		$target_path = 'upload/' . date('Y/m/d/');
		$targetPath = public_path() . DIRECTORY_SEPARATOR . $target_path;
		if (!is_dir($targetPath)) {
			if (!mkdir($targetPath, 0777, 1)) {
				return $this->response('无法建立上传目录');
			}
		}
		$new_filename = uniqid(mt_rand()) . '.jpg';
		$targetFile = $targetPath . $new_filename;
		if (file_put_contents($targetFile, $imagedata)) {
			file_exists($row->pic) AND unlink($row->pic);
			$row->pic = $target_path . $new_filename;
			$row->save();
			$this->toQiniutask($row);
			$row->pic = asset($row->pic);
			return $this->response($row);
		}
		return $this->response('操作失败');
	}

	/**
	 * 删除记录
	 */
	public function postDelete(Request $request) {
		$id = intval($request->input('id'));
		$row = Pageadver::find($id);
		if (empty($row)) {
			return $this->error('无效的记录');
		}
		file_exists($row->pic) AND unlink($row->pic);
		$row->delete();
		return $this->response($id);
	}

	/**
	 * 加入七牛上传任务队列
	 */
	private function toQiniutask(Pageadver $row) {
		$task = new Qiniutask;
		$task->master_id = $row->id;
		$task->table_name = 'pageadver';
		$task->field_name = 'pic';
		$task->file_path = realpath($row->pic);
		$task->target_path = '';
		$task->need_upload = 1;
		$task->need_convert = 0;
		$task->status = 0; //-1:任务处理失败,0:待处理任务,1:任务处理中,2:任务处理结束,3:任务完成
		$task->save();

		$this->dispatch(new QiniuUploadFile($task));
		return true;
	}

}
